package com.kcloud.ms.gateway.openid.controller;

import org.springframework.security.openid.OpenIDAuthenticationToken;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController(value = "OpenIDController")
public class UIController {

//    @GetMapping(value = "/openid_user")
//    @ResponseBody
//    public Map<String, Object> openidUser(Principal user, HttpServletRequest request, HttpServletResponse response, OAuth2AuthenticationToken authentications) {
//        Map<String, Object> result = new HashMap<>();
//        if (user instanceof Authentication) {
//            Authentication authentication = (Authentication) user;
//            DefaultOidcUser principal = (DefaultOidcUser) authentication.getPrincipal();
//            OidcIdToken idToken = principal.getIdToken();
//            Map<String, Object> userInfoClaims = idToken.getClaims();
//            // 保存用户信息,字符串类型
//            result.put("authenticated", authentication.isAuthenticated());
//            String name = (String)userInfoClaims.get("sub");
//            String[] departmentCodAndUserid = name.split("\\|");
//            request.getSession().setAttribute(AuthServerConstants.SESSION_KEY_LOGINID, departmentCodAndUserid[1]);
//            request.getSession().setAttribute(AuthServerConstants.SESSION_KEY_USERID, departmentCodAndUserid[1]);
//            //TODO 未设置用记姓名，需要自行扩展
////            request.getSession().setAttribute(AuthServerConstants.SESSION_KEY_USERNAME, extendAccountInfo.getName());
//            request.getSession().setAttribute(AuthServerConstants.SESSION_KEY_DEPARTID,
//                    departmentCodAndUserid[0]);
//            //TODO 未设置管理范围，需要根据业务自行扩展
////            request.getSession().setAttribute(AuthServerConstants.SESSION_KEY_SCOPECODE,
////                    extendAccountInfo.getScopeCodes() != null && extendAccountInfo.getScopeCodes().length > 0
////                            ? extendAccountInfo.getScopeCodes()[0]
////                            : "");
////            request.getSession().setAttribute(AuthServerConstants.SESSION_KEY_UNITSCOPECODE,
////                    extendAccountInfo.getUnitScopeCodes() != null && extendAccountInfo.getUnitScopeCodes().length > 0
////                            ? extendAccountInfo.getUnitScopeCodes()[0]
////                            : "");
//            //TODO 使用中铁建默认角色，ROLE_USER，可根据业务自行修改
//            request.getSession().setAttribute(AuthServerConstants.SESSION_KEY_ROLES, principal.getAuthorities().stream()
//                    .map(GrantedAuthority::getAuthority).collect(Collectors.joining(",")));
//        }
//        return result;
//    }
    @GetMapping("/message")
    public String message() {
        return "secret message";
    }
    @GetMapping("/user2")
    @ResponseBody
    public OpenIDAuthenticationToken show(Model model, OpenIDAuthenticationToken authentication) {
        model.addAttribute("authentication", authentication);
        return authentication;
    }
}
